package cn.tsoft.taobao.ui;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


import cn.tsoft.taobao.adapter.TradesAdapter;
import cn.tsoft.taobao.db.TaoBaoDataManager;


import android.app.ExpandableListActivity;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.widget.ExpandableListAdapter;

public class TradeOrder  extends ExpandableListActivity {

	private ProgressDialog progressDialog;
	private ExpandableListAdapter expAdapter;
	private static List<Map<String, Object>> parentData = new ArrayList<Map<String, Object>>();
	private static List<List<Map<String, String>>> childData = new ArrayList<List<Map<String, String>>>();
	Map<String, Object>   dataMap;
	
	private Handler handler = new Handler();
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getOrders();
		registerForContextMenu(getExpandableListView());
	}

	private void getOrders() {
			progressDialog = ProgressDialog.show(TradeOrder.this, "请稍等...",
					"获取交易订单的信息...", true);
			new Thread() {
				public void run() {
					try {
						dataMap =TaoBaoDataManager.getTradeOrderList();
						if (dataMap != null && dataMap.size() > 0) {
							updateList();
						}
					} catch (Exception e) {
					}
					progressDialog.dismiss();
				}
			}.start();
	}
	
	private void updateList() {
		handler.post(new Runnable() {
			public void run() {
				getData();
				expAdapter = new TradesAdapter(TradeOrder.this, parentData,
						R.layout.trade_order_row, new String[] { "state",
								"num" }, new int[] {
								R.id.tradeOrderState, R.id.tradeOrderNum }, childData,
						R.layout.trade_order_body, new String[] { "image", "productName","productNum","payment" },
						new int[] { R.id.trade_state_image, R.id.product_name,R.id.product_num, R.id.payment});
				setListAdapter(expAdapter);
			}
		});
	}
	
	
	private void getData() {
		parentData.clear();
		childData.clear();
		
		parentData = (List<Map<String, Object>>) dataMap.get("mainList");
		childData = (List<List<Map<String, String>>>)  dataMap.get("child");
	}
	
	
}
